

do_table_A8<-function( drop_cols = c("id_var"  ,"zihui_bikur", "zihui_ishpuz_bikur" ) ) { 
  ## Groups of admissions (by death period) splitted by procedures groups (exclusive)
  admissions<- get_table_4() %>%  select(-drop_cols)
  #create nested dataframe to work with all groups in one place
  adm_tbl <- admissions %>%
    group_by(Urgent) %>%
    nest()
  # map through all group dataframes, creating summary dataframes
  adm_tbl <- adm_tbl %>%
    mutate(means_dt = map(data, ~group_by(.x, group)) %>% 
             map(~add_tally(.x)) %>%
             map(~summarise_all(.x, mean, na.rm=T)) %>%
             map(~select(.x, group:Other, n)) %>%
             map(~ungroup(.x))) %>% 
    mutate(means_dt = map(data, ~add_tally(.x)) %>%
             map2(.x=.,.y=means_dt, # map2 here is needed to use original dataframe for adding totals
                  .f= ~bind_rows(.y, summarise_at(.x, 1:6, mean, na.rm=T) %>% 
                                   mutate(group='All') %>%
                                   mutate(n=sum(.y$n)))) %>%
             map(~mutate_at(.x, 2:7, funs(round(., 3)))) %>%
             map(~rename(.x,'Time Before Death' = group, 'N of Admissions' = n)))
  
  adm_tbl <- adm_tbl %>% arrange(Urgent)
  
  ### Unplanned Admissions:  - planned   unplanned
  proce_two <- bind_rows(adm_tbl$means_dt)  %>% 
    rename(Maintenance = Maintanance) %>%
    mutate_at( vars(Maintenance:Other), funs( format(round(.*100,1) ,nsmall = 1 ) )) %>%
    mutate (`N of Admissions` = scales::comma(`N of Admissions`) )
  
  proce_two[5,]$`Time Before Death`<- "All Planned"
  proce_two[10,]$`Time Before Death`<- "All Unplanned"
  proce_two[1,]$`Time Before Death`<- "Last month"
  proce_two[6,]$`Time Before Death`<- "Last month"
  
  return(proce_two)
  
} 



### procedure analysis :  -------------------------------------------


get_table_4_intensity <- function( con,
                                   data_adm= dt_adm[max_cost_row == 1 ]) {
 
  
  admissions_ward <- tbl(con, dbplyr::in_schema("table")) %>%
    mutate(zihui_bikur = as.character(zihui_ishpuz_bikur)) %>% 
    collect() %>% 
    data.table()
  
  
  dt_admissions_ward<- merge(y = admissions_ward , 
                             x = data_adm[,.(zihui_bikur,id_var,test =1 )], 
                        by = c("zihui_bikur","id_var"),
                        all.y = T,
                        all.x = F
  )
  
  names(admissions_ward)
  return(   dt_admissions_ward[test==1,-c("test")]   )
}


## Groups of admissions (by death period) splitted by procedures groups (exclusive)
do_table_A8_intensity<-function(intensity_wards = c("oncology"  ,"internal_medicine", "geriatry" ,"rehabilitation" ),
                                drop_cols = c("id_var","zihui_bikur",  "zihui_ishpuz_bikur" )) { 
  
  admissions_ward<- get_table_4_intensity() %>%
    select(-drop_cols) %>% 
    mutate(Intensity = ifelse(ward_top10%in%intensity_wards,
                              "Low","High"))
  
  
  #create nested dataframe to work with all groups in one place
  adm_tbl_I <- admissions_ward %>%   select(-c("ward_top10")) %>%
    group_by(Intensity) %>%
    nest()
  
  # map through all group dataframes, creating summary dataframes
  adm_tbl_I<- adm_tbl_I %>%
    mutate(means_dt = map(data, ~group_by(.x, group)) %>% 
             map(~add_tally(.x)) %>%
             map(~summarise_all(.x, mean, na.rm=T)) %>%
             map(~select(.x, group:Other, n)) %>%
             map(~ungroup(.x))) %>% 
    mutate(means_dt = map(data, ~add_tally(.x)) %>%
             map2(.x=.,.y=means_dt, # map2 here is needed to use original dataframe for adding totals
                  .f= ~bind_rows(.y, summarise_at(.x, 1:6, mean, na.rm=T) %>% 
                                   mutate(group='All') %>%
                                   mutate(n=sum(.y$n)))) %>%
             map(~mutate_at(.x, 2:7, funs(round(., 3)))) %>%
             map(~rename(.x,'Time Before Death' = group, 'N of Admissions' = n)))
  
  adm_tbl_I <- adm_tbl_I %>% arrange(rev(Intensity))
  
  ### Unplanned Admissions:  - planned   unplanned
  proce_two_I <- bind_rows(adm_tbl_I$means_dt)  %>% 
    rename(Maintenance = Maintanance) %>%
    mutate_at( vars(Maintenance:Other), funs( format(round(.*100,1) ,nsmall = 1 ) )) %>%
    mutate (`N of Admissions` = scales::comma(`N of Admissions`) )
  
  proce_two_I[5,]$`Time Before Death`<- "All Low Intensity"
  proce_two_I[10,]$`Time Before Death`<- "All High Intensity"
  proce_two_I[1,]$`Time Before Death`<- "Last month"
  proce_two_I[6,]$`Time Before Death`<- "Last month"
  
  
  return( proce_two_I)
}

tab_Procedures_planned_intensity <- function(urgent = do_table_A8() ,
                           intensity = do_table_A8_intensity() )  {
  
  table<- rbind(urgent, intensity)
  
  write.csv(table,"Procedures_planned_intensity.csv")
  
  invisible(Hmisc::latex(
    table[,-1],
    file = paste0("Procedures_planned_intensity.tex"),
    center = 'centering',
    
    n.cgroup = c(6,1 ),
    cgroup   = c("Procedure Type, Admission With Any (\\%)",""),
    n.rgroup = c(5,5,5,5),
    rgroup = c("A. Planned Admissions", "B. Unplanned Admissions","C. Low Intensity", "D. High Intensity"),
    rowname =table$`Time Before Death`,
    rowlabel = "",
    col.just = c("l", rep.int("r", 7)),
    extracolheads = c("(1)", "(2)", "(3)", "(4)","(5)","(6)","(7)"), 
    extracolsize = "normalsize"
  ))
  table
}
  

